package nccloud.web.uapbd.ref.customer;

import java.util.Map;
import nc.vo.pub.lang.UFBoolean;
import nccloud.framework.web.processor.refgrid.RefQueryInfo;
import nccloud.framework.web.ui.meta.RefMeta;
import nccloud.pubitf.platform.db.SqlParameterCollection;
import nccloud.web.refer.DefaultGridRefAction;



public class CustMaterialGridRef
  extends DefaultGridRefAction
{
  public String getExtraSql(RefQueryInfo paramRefQueryInfo, RefMeta paramRefMeta) {
    Map<String, String> queryCondition = paramRefQueryInfo.getQueryCondition();

    
    boolean isShowDisabledData = (queryCondition.get("isShowDisabledData") == null) ? false : UFBoolean.valueOf((String)queryCondition.get("isShowDisabledData")).booleanValue();
    
    String str = " ";
    if (queryCondition != null && queryCondition.get("pk_customer") != null && 
      !"".equals(((String)queryCondition.get("pk_customer")).trim()))
    {
      str = str + " and (bd_custmaterial.pk_customer=? or bd_custmaterial.pk_customer=(select maincustom from bd_custmatersub where subcustom=?)) ";
      if(queryCondition.get("materialid") != null && !"".equals(((String)queryCondition.get("materialid")).trim())) {
    	  str = str + " and bd_custmaterial.materialid=? ";
      }
    }
    
    if (!isShowDisabledData) {
      str = str + " and bd_custmaterial.enablestate=2 ";
    } else {
      str = str + " and (bd_custmaterial.enablestate=2 or bd_custmaterial.enablestate=3)";
    }
    return str;
  }

  
  public SqlParameterCollection getExtraSqlParameter(RefQueryInfo refQueryInfo, RefMeta refMeta) {
    Map<String, String> queryCondition = refQueryInfo.getQueryCondition();
    
    if (queryCondition != null && queryCondition.get("pk_customer") != null && 
      !"".equals(((String)queryCondition.get("pk_customer")).trim())) {
      SqlParameterCollection sc = new SqlParameterCollection();
      sc.addVarChar((String)queryCondition.get("pk_customer"));
      sc.addVarChar((String)queryCondition.get("pk_customer"));
      if (queryCondition.get("materialid") != null &&  !"".equals(((String)queryCondition.get("materialid")).trim())) {
    	  sc.addVarChar((String)queryCondition.get("materialid"));
      }
      return sc;
    }
    
    return null;
  }


  
  public String getOrderSql(RefQueryInfo refQueryInfo, RefMeta refMeta) { return " order by bd_custmaterial.code "; }


  
  public RefMeta getRefMeta(RefQueryInfo refQueryInfo) {
    RefMeta meta = new RefMeta();
    meta.setCodeField("bd_custmaterial.code");
    meta.setNameField("bd_custmaterial.name");
    meta.setMutilLangNameRef(true);
    meta.setPkField("bd_custmaterial.pk_custmaterial");
    meta.setTableName(" bd_custmaterial left join bd_material on bd_custmaterial.materialid=bd_material.pk_material ");
    
    meta.setExtraFields(new String[] { "bd_custmaterial.code", "bd_custmaterial.name", "bd_material.code as material_code", "bd_material.name as material_name", "bd_material.version", "bd_material.materialspec as material_spec" });

    
    return meta;
  }
}